package com.lajiaomian.springbootruijiwaimai.service.impl.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lajiaomian.springbootruijiwaimai.dto.SetmealDto;
import com.lajiaomian.springbootruijiwaimai.mapper.SetmealMapper;
import com.lajiaomian.springbootruijiwaimai.pojo.Category;
import com.lajiaomian.springbootruijiwaimai.pojo.Setmeal;
import com.lajiaomian.springbootruijiwaimai.pojo.SetmealDish;
import com.lajiaomian.springbootruijiwaimai.service.impl.CategoryService;
import com.lajiaomian.springbootruijiwaimai.service.impl.SetmealDishService;
import com.lajiaomian.springbootruijiwaimai.service.impl.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lajiaomian
 * @create 2022-08-13-10:28
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public Long CountCategoryId(Long categoryId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("category_id",categoryId);
        return count(wrapper);
    }

    @Override
    public void addSetmealDto(SetmealDto setmealDto) {
        //先将属性设置进setmeal里
        this.save(setmealDto);
        Long id = setmealDto.getId();
        //获取setmealdish
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish item: setmealDishes) {
            //给item对象赋值上setmealId
            item.setSetmealId(id);
        }
        //setmealDishes保存到数据库
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public SetmealDto getSetmealDtoByid(Long id) {
        //根据id获取Setmeal对象
        Setmeal setmeal = this.getById(id);
        //获取SetmealDish的List
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("setmeal_id",id);
        List list = setmealDishService.list(wrapper);
        //新建SetmealDto对象
        SetmealDto setmealDto = new SetmealDto();
        //将查询出来的值赋给setmealDto
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
        //删除数据库中原有的数据
        Long id = setmealDto.getId();
        //删除setmeal表数据
        this.removeById(id);
        //删除setmealdish表数据
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("setmeal_id",id);
        setmealDishService.remove(wrapper);
        //重新插入数据
        //给setmealdish插入
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish item:setmealDishes) {
            item.setSetmealId(id);
        }
        setmealDishService.saveBatch(setmealDishes);
        //给setmeal插入数据
        this.save(setmealDto);
    }


}
